package com.kongt;

import com.kongt.download.DownTaskManager;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.event.ContextRefreshedEvent;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;

@ComponentScan
public class Aplication implements ApplicationListener<ContextRefreshedEvent> {


    static String[] _startArgs = null;

    /**
     * bean容器
     */
    static AnnotationConfigApplicationContext annotationConfigApplicationContext = null;


    public static void main(String[] args) throws InterruptedException, IOException {
        _startArgs = args;
        if (null == args || args.length == 0) {
            String[] configStrs = new String[3];
            Scanner scanner = new Scanner(System.in);
            System.out.println("please enter the download Url:");
            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
            configStrs[0] = "-DdownloadUrl=" + scanner.nextLine();
            System.out.println("please enter the thread count:");
            configStrs[1] = "-DthreadC=" + scanner.nextLine();
            System.out.println("please enter the download path:");
            configStrs[2] = "-Dpath=" + scanner.nextLine();
            _startArgs = configStrs;
        }
        //容器初始化
        annotationConfigApplicationContext = new AnnotationConfigApplicationContext(Aplication.class);
    }


    public static AnnotationConfigApplicationContext getAnnotationConfigApplicationContext() {
        return annotationConfigApplicationContext;
    }


    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        try {

            DownTaskManager bean = contextRefreshedEvent.getApplicationContext().getBean(DownTaskManager.class);
            bean.download();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


    public static String[] get_startArgs() {
        return _startArgs;
    }
}
